//
//  SAPaymentVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/4/15.
//

#import "SAPaymentVC.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SAPaymentCell.h"
#import "SACommoditySpecialFeesModel.h"

#import "SAPayment1VC.h"
#import "SAPayment2VC.h"
#import "SAPayment3VC.h"

@interface SAPaymentVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UILabel *proportionLab;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *marr;
@property (nonatomic, strong) NSMutableArray *dataMarr;
@property (nonatomic, strong) SAOverallSetting *overallSetting;

@end

@implementation SAPaymentVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.marr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAPaymentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPaymentCell" forIndexPath:indexPath];
    [cell refreshWithTitle:self.marr[indexPath.section] AndContent:self.dataMarr[indexPath.section]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WeakSelf
    if (indexPath.section == 0) {
        self.overallSetting.settlementMethod.value = [NSString stringWithFormat:@"0,0"];
        [self refreshData];
    }else if (indexPath.section == 1) {
        SAPayment1VC *pushVC = [[SAPayment1VC alloc] init];
        pushVC.selectBlock = ^(NSString * _Nonnull content) {
            weakSelf.overallSetting.settlementMethod.value = [NSString stringWithFormat:@"1,%@",content];
            [weakSelf refreshData];
        };
        [self.navigationController pushViewController:pushVC animated:YES];
    }else if (indexPath.section == 2) {
        SAPayment2VC *pushVC = [[SAPayment2VC alloc] init];
        pushVC.selectBlock = ^(NSString * _Nonnull content) {
            weakSelf.overallSetting.settlementMethod.value = [NSString stringWithFormat:@"2,%@",content];
            [weakSelf refreshData];
        };
        [self.navigationController pushViewController:pushVC animated:YES];
    }else if (indexPath.section == 3) {
        SAPayment3VC *pushVC = [[SAPayment3VC alloc] init];
        pushVC.selectBlock = ^(NSString * _Nonnull content) {
            weakSelf.overallSetting.settlementMethod.value = [NSString stringWithFormat:@"3,%@",content];
            [weakSelf refreshData];
        };
        [self.navigationController pushViewController:pushVC animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(45);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

#pragma mark - event
-(void)refreshData {
    [self.overallSetting requestByUpdateSettlementTypeSuccessBlock:^(id  _Nonnull response) {
        [self.overallSetting requestByGetOverallSettingWithSuccessBlock:^(id  _Nonnull response) {
            self.overallSetting = [SAOverallSetting shareInstance];
            NSArray *arr = [self.overallSetting.settlementMethod.value componentsSeparatedByString:@","];
            if (arr.count > 1) {
                NSString *type = arr[0];
                NSString *value = arr[1];
                self.dataMarr[0] = @"settlement";
                self.dataMarr[1] = @"";
                self.dataMarr[2] = @"";
                self.dataMarr[3] = @"";
                if ([type isEqualToString:@"0"]) {
                    self.dataMarr[0] = @"noSettlement";
                }else if ([type isEqualToString:@"1"]) {
                    self.dataMarr[1] = value;
                }else if ([type isEqualToString:@"2"]) {
                    self.dataMarr[2] = value;
                }else if ([type isEqualToString:@"3"]) {
                    self.dataMarr[3] = value;
                }
            }
            [self.tableView reloadData];
            !self.refreshBlock ?: self.refreshBlock();
        } failBlock:^(NSString * _Nonnull errorMessage) {
            
        }];
    } failBlock:^(NSString * _Nonnull errorMessage) {
        
    }];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"结算方式"];
    [self setBgImage:nil];
    self.view.backgroundColor = kBgColor;
    self.overallSetting = [SAOverallSetting shareInstance];
    
    [self.view addSubview:self.proportionLab];
    [self.view addSubview:self.tableView];
    
    NSArray *arr = [self.overallSetting.settlementMethod.value componentsSeparatedByString:@","];
    if (arr.count > 1) {
        NSString *type = arr[0];
        NSString *value = arr[1];
        self.dataMarr[0] = @"settlement";
        if ([type isEqualToString:@"0"]) {
            self.dataMarr[0] = @"noSettlement";
        }else if ([type isEqualToString:@"1"]) {
            self.dataMarr[1] = value;
        }else if ([type isEqualToString:@"2"]) {
            self.dataMarr[2] = value;
        }else if ([type isEqualToString:@"3"]) {
            self.dataMarr[3] = value;
        }
    }
    [self.tableView reloadData];

}

-(UILabel *)proportionLab {
    if (!_proportionLab) {
        _proportionLab = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(20), kScaleNum(StatusHeight), kScaleNum(300), kScaleNum(35))];
        _proportionLab.text = @"调配货时默认结算方式";
        _proportionLab.textAlignment = NSTextAlignmentLeft;
        _proportionLab.textColor = kLightgGrayColor;
        _proportionLab.font = kFont(16);
    }
    return _proportionLab;
}

#pragma mark - lazy
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 1), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 1) - SafeFooterHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.bounces = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAPaymentCell class] forCellReuseIdentifier:@"SAPaymentCell"];
    }
    return _tableView;
}

-(NSMutableArray *)marr {
    if (!_marr) {
        _marr = [[NSMutableArray alloc] initWithObjects:@"不结算（调配单据不显示单价及金额）", @"加收固定比例", @"加收固定金额", @"按固定金额结算", nil];
    }
    return _marr;
}

-(NSMutableArray *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [[NSMutableArray alloc] initWithObjects:@"", @"", @"", @"", nil];
    }
    return _dataMarr;
}

@end
